package com.bolt.report.business.modules.report.controller;


import com.bolt.common.utils.StrUtil;
import com.bolt.core.view.components.FormItem;
import com.bolt.report.business.modules.report.service.FormSelectItemBindData;
import com.bolt.report.business.modules.report.service.FormSelectTreeBindData;
import com.bolt.support.ui.tree.helper.TreeRenderHelper;
import com.bolt.support.ui.tree.vo.UITreeNode;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("report/data")
@RequiredArgsConstructor
@Api(tags = "控制数据访问")
public class ReportSelectLoadController {

    @Autowired
    private FormSelectTreeBindData formSelectTreeBindData;

    @Autowired
    private FormSelectItemBindData formSelectItemBindData;

    @GetMapping(value = "/getTree")
    public List<UITreeNode> getTree(@RequestParam(name = "id", required = false) String id, @RequestParam Map<String, String> params) {
        boolean loadAllNodes = StrUtil.isEmpty(params.get("loadAllNodes")) ? false : Boolean.parseBoolean(params.get("loadAllNodes"));
        List<UITreeNode> dataArray = new ArrayList<>();
        TreeRenderHelper.buildNodesTreeView(formSelectTreeBindData, id, params, dataArray, loadAllNodes);
        return dataArray ;
    }

    @GetMapping(value = "/getData")
    public Map<String, String> getData(@RequestParam Map<String, String> params) {
        FormItem formItem =formSelectItemBindData.getFormItem(params);
        return formSelectItemBindData.load(formItem,params);
    }
}